package netty;

import io.netty.bootstrap.Bootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import netty.handler.NettyClientHandler;

/**
 * @author ：朱博
 * @description：Netty客户端
 * @date ：2021/4/12 23:27
 */
public class NettyClient {

    public static void main(String[] args) throws InterruptedException {
        //客户端的EventLoop
        NioEventLoopGroup eventExecutors = new NioEventLoopGroup();

        Bootstrap bootStrap = new Bootstrap();

        bootStrap.group(eventExecutors)
                .channel(NioSocketChannel.class)
                .handler(new ChannelInitializer<SocketChannel>() {

                    @Override
                    protected void initChannel(SocketChannel socketChannel) throws Exception {
                        socketChannel.pipeline().addLast(new NettyClientHandler());
                    }
                });
        System.err.println("客户端已经开启......Loading====>>>>>>>");

        //hannelFuture 这个设计到Netty 的异步模型
        ChannelFuture connect = bootStrap.connect("127.0.0.1", 6668).sync();
        //给关闭通道的时间增加一个监听
        connect.channel().closeFuture().sync();
    }
}
